/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Arduino;

import Clases.SignosVitales;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.Enumeration;

/**
 *
 * @author xavier
 */
public class UtilArduino implements SerialPortEventListener, Serializable {

    private BufferedReader input = null;
    private SerialPort serialPort;
    private final String PORT_NAME = "COM3";
    private static final int TIME_OUT = 2000;
    private static final int DATA_RATE = 115200;

    double hcg;
    int pr;
    int sp;
    private boolean flag;
    private double tiempo;
    private SignosVitales sv;

    public void ArduinoConnection() {
        CommPortIdentifier portId = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
        while (portId == null && portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            if (PORT_NAME.equals(currPortId.getName())) {
                portId = currPortId;
                break;
            }
        }
        if (portId == null) {
            return;
        }
        try {
            try {
                serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);
            } catch (PortInUseException e) {
                System.out.println(e.getMessage());
            }
            serialPort.setSerialPortParams(DATA_RATE,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void setInput(BufferedReader input) {
        this.input = input;
    }

    public SerialPort getSerialPort() {
        return serialPort;
    }

    
    public int getPr() {
        return pr;
    }

    public int getSp() {
        return sp;
    }

//    @Override
    public void serialEvent(SerialPortEvent spe) {
        try {
            switch (spe.getEventType()) {
                case SerialPortEvent.DATA_AVAILABLE:
                    if (input == null) {
                        input = new BufferedReader(
                                new InputStreamReader(
                                        serialPort.getInputStream()));
                    }
                    String inputLine = input.readLine();
                    String[] datos = inputLine.split(";");
                    if (datos.length == 4) {
                        if (Double.parseDouble(datos[0]) >= 0 && Double.parseDouble(datos[0]) <= 3 && !flag) {
                            flag = true;
                        }
                        if (flag) {
                            tiempo = Double.parseDouble(datos[0]);
                            hcg = Double.parseDouble(datos[1]);
                            pr = Integer.parseInt(datos[2]);
                            sp = Integer.parseInt(datos[3]);                            
                            sv.setTiempo(tiempo);
                            sv.setHcg(hcg);
                            sv.setPr(pr);
                            sv.setPs(sp);                                                        
                            //FuncionesSignosVitales.insertar(sv);                                                        
                        }
                    }                    
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}
